package org.example;

import java.util.Scanner;

public class CounterDemo {
    static class Counter {
        public volatile int flag = 0;
    }
    public static void main(String[] args) throws InterruptedException {
        Counter counter = new Counter();
        Thread t1 = new Thread(() -> {
           for (int i=0;i<1000;i++){
               counter.flag++;
           }
            System.out.println("循环结束!");
        });
        Thread t2 = new Thread(() -> {
            for (int i=0;i<1000;i++){
                counter.flag++;
            }
        });
        t1.start();
        t2.start();
        t1.join();
        t2.join();
        System.out.println(counter.flag);
    }
}
